package com.you.concurrent.waitnotify;

public class WaitNotifyDemo implements Runnable {

    static int num = 0;

    public Object obj = null;

    public int flag = 0;

    public WaitNotifyDemo(Object obj, int flag) {
        this.obj = obj;
        this.flag = flag;
    }

    public void increase() {
        synchronized (obj) {
            System.out.println("-----start increase---");
            try {
                obj.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("-----end increase---");
        }
    }

    public void deduct() {
        synchronized (obj) {
            System.out.println("-----start deduct---");
            obj.notifyAll();
            try {
                Thread.sleep(5000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("-----end deduct---");
        }
    }

    @Override
    public void run() {
        if (flag == 0) {
            increase();
        } else {
            deduct();
        }
    }

    public static void main(String[] args) {
        Object obj = new Object();
        new Thread(new WaitNotifyDemo(obj, 0)).start();
        new Thread(new WaitNotifyDemo(obj, 1)).start();
    }

}
